﻿/********************************************************************************
** 作者： Mick
** 邮箱： zheng_jinfan@126.com
** 主页： http://www.zhengjinfan.cn
** 创始时间：2016-2-17
** 描述：
**      [辅助类]查询所属区域辅助类
*********************************************************************************/

using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using BestEasyCMS.Framework.Extension;

namespace BestEasyCMS.Framework.Util
{
    /// <summary>
    /// [辅助类]查询所属区域辅助类
    /// </summary>
    public sealed class AttributableAreaUtil
    {
        #region 根据单一模式构建类的对象

        //私有构造函数

        private static AttributableAreaUtil _object; //静态变量
        private readonly static Object _obj = new object();
        /// <summary>
        /// 私有构造函数
        /// </summary>
        private AttributableAreaUtil()
        {
        }

        /// <summary>
        /// 提供类的实例属性
        /// </summary>
        public static AttributableAreaUtil Instance
        {
            get
            {
                lock (_obj)
                {
                    return _object ?? (_object = new AttributableAreaUtil());
                }
            }
        }

        #endregion

        /// <summary>
        /// 获取IP归属地区名
        /// </summary>
        /// <param name="ip">IP地址</param>
        /// <returns>国家-区域-省-市-区/县</returns>
        public string GetAreaNameByIp(string ip)
        {
            string url = "http://ip.taobao.com/service/getIpInfo.php?ip={0}"; //淘宝IP查询接口
            url = url.FormatWith(ip);
            var client = new WebClient
            {
                Encoding = Encoding.UTF8
            };
            string result = client.DownloadString(url);
            string address = "";

            if (!result.IsEmpty())
            {
                address =
                    Regex.Match(result, "\"country\":\"[A-z0-9]+\"").Value.Replace("\"country\":", "").Replace("\\u",
                                                                                                               @"/u").
                        Trim('"').ToGb(); //国家
                address += "-" +
                           Regex.Match(result, "\"area\":\"[A-z0-9]+\"").Value.Replace("\"area\":", "").Replace("\\u",
                                                                                                                @"/u").
                               Trim('"').ToGb(); //区域
                address += "-" +
                           Regex.Match(result, "\"region\":\"[A-z0-9]+\"").Value.Replace("\"region\":", "").Replace(
                               "\\u", @"/u").Trim('"').ToGb(); //省
                address += "-" +
                           Regex.Match(result, "\"city\":\"[A-z0-9]+\"").Value.Replace("\"city\":", "").Replace("\\u",
                                                                                                                @"/u").
                               Trim('"').ToGb(); //市
                address += "-" +
                           Regex.Match(result, "\"county\":\"[A-z0-9]+\"").Value.Replace("\"county\":", "").Replace(
                               "\\u", @"/u").Trim('"').ToGb(); //区/县
            }


            return address;
        }
    }
}